#include "be_filesystem.h"

using namespace boost::filesystem;

BeFilesystem* BeFilesystem::Instance () 
{
	static BeFilesystem t;
	return &t; // _instance isn't needed in this case
}

BeFilesystem::BeFilesystem()
{
}

bool BeFilesystem::load( BeFile& befile, const std::string& file )
{
	// search root paths for file
	for ( unsigned int i=0; i < berootpaths.list.size(); i++ )
	{
		std::string fullfilename( berootpaths.list[i] );
		fullfilename.append( file );
		std::cerr << "checking " << fullfilename << std::endl;
		
		if ( boost::filesystem::exists( fullfilename ) )
		{
			// TEST
			befile.setFilename( fullfilename );
			return true;
		}
	}

	std::cerr << "cannot find file (" << file << ")" << std::endl;
	return false;
}

void BeFilesystem::save(const std::string &filename, const std::string& content)
{
	std::fstream file_op(filename.c_str(),std::ios::out);
	file_op << content;
	file_op.close();
}

void BeFilesystem::rm(const std::string &filename)
{
	if( remove( filename.c_str() ) != 0 )
		std::cerr << "Error deleting file" << std::endl;
// 	else
// 		cerr << "file succesfully deleted" << endl;
}

BeFilesystem::~BeFilesystem()
{
}
